package com.thinkingstar.iads.cs.sys.entity.dao;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.StringUtil;
import com.thinkingstar.iads.cs.sys.entity.*;
import org.apache.poi.ss.formula.functions.T;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.List;

@IocBean(args = {"refer:dao"})
public class DaoSysMngSysUser extends EntityRepositoryServiceImpl<DaoSysMngSysUser> {
    private static final String ADMIN = "系统登录";

    public DaoSysMngSysUser(Dao dao) {
        super(dao);
    }

    /**
     * 获取用户所属专业
     * @param user_id
     * @return
     */
    public List<SysMajor> getMajorByUserId(long user_id){
        String executeSql="select m.* from dc_sys_user_major user_m left join dc_sys_major m on user_m.majorid=m.id where user_m.userid=@userid";
        Sql sql = Sqls.create(executeSql);
        sql.params().set("userid", user_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SysMajor.class));
        dao().execute(sql);
        return sql.getList(SysMajor.class);
    }

    /**
     * 根据用户id获取用户角色信息
     * @param user_id
     * @return
     */
    public List<SysRoleResource> getRoleByUserId(long user_id){
        String roleExecuteSql="select rr.*,r.name as roleName from dc_sys_user_role user_r left join dc_sys_role r on user_r.roleid=r.id left join dc_sys_role_resource rr on r.id=rr.roleid where user_r.userid=@userid";
        Sql sql = Sqls.create(roleExecuteSql);
        sql.params().set("userid", user_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SysRoleResource.class));
        dao().execute(sql);
        return sql.getList(SysRoleResource.class);
    }


    /**
     * 根据用户id获取用户拥有角色信息
     * @param user_id
     * @return
     */
    public List <SysRole> getUserRoleByUserId(long user_id){
        String user_role_sql = "select r.* from dc_sys_user_role user_r inner join dc_sys_role r ON user_r.roleid = r.id where user_r.userid = @userid ";
        Sql sql = Sqls.create(user_role_sql);
        sql.params().set("userid", user_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SysRole.class));
        dao().execute(sql);
        return sql.getList(SysRole.class);
    }

    /**
     * 判断是否系统管理员(是否拥有管理员权限)
     * @param user_id
     * @return
     */
    public Boolean is_admin(long user_id){
        String is_admin_sql = " select sur.* , sr.name roleName from dc_sys_user_role sur inner join dc_sys_role sr on sur.roleid = sr.id where sur.userid = @userid";
        Sql sql = Sqls.create(is_admin_sql);
        sql.params().set("userid", user_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SysUserRole.class));
        dao().execute(sql);
        List <SysUserRole> list = sql.getList(SysUserRole.class);
        for(SysUserRole sysUserRole : list){
            if(ADMIN.equals(sysUserRole.getRoleName())){
                return true;
            }
        }
        return false;
    }


    /**
     * 根据用户id获取该用户所有资源id
     * @param user_id
     * @return
     */
    public List <String> getResourceIdByUserId(long user_id){
        String user_resource_sql = " select distinct(sr.id) id "
                + " from dc_sys_user_role sus , dc_sys_role_resource srr , dc_sys_resource sr where sus.roleid = srr.roleid and srr.resourceid = sr.id "
                + " and sus.userid = @userid";
        Sql sql = Sqls.create(user_resource_sql);
        sql.params().set("userid", user_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(String.class));
        dao().execute(sql);
        return sql.getList(String.class);
    }

    /**
     * 根据上级资源id查询管理员的所有下级资源
     * @return
     */
    public List<SysResource> getResourceByAdminAndParentResId(String res_id){
        String resource_sql = " select * from dc_sys_resource where pid = @pid ";
        Sql sql = Sqls.create(resource_sql);
        sql.params().set("pid", res_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SysResource.class));
        dao().execute(sql);
        return sql.getList(SysResource.class);
    }


    /**
     * 根据角色资源集合以及上级资源id查询资源
     * @param id_list
     * @param res_id
     * @return
     */
    public List <SysResource> getResourceByUserIdAndResId(List <String> id_list , String res_id){
        if(id_list == null || id_list.isEmpty()){
            return null;
        }
        String idString = StringUtil.getIdssplit(id_list);
        String resourceSql = " select * from dc_sys_resource where pid = @pid and id in "+idString;
        Sql sql = Sqls.create(resourceSql);
        sql.params().set("pid", res_id);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SysResource.class));
        dao().execute(sql);
        return sql.getList(SysResource.class);
    }


    /**
     * 根据角色获取资源
     * @param role_id
     * @return
     */
    public List <SysResource> getResourceByRole(long role_id){
        String resourceSql = " select a.* from dc_sys_resource a inner join dc_sys_role_resource b on a.id = b.resourceid where b.roleid = @roleid";
        Sql sql = Sqls.create(resourceSql);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao().getEntity(SysResource.class));
        dao().execute(sql);
        return sql.getList(SysResource.class);
    }

}
